<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .box {
      width: 200px;
      height: 300px;
      background-color: aqua;
    }

    .box .close {
      width: 15px;
      height: 20px;
      background-color: red;
      position: absolute;
      top: 0;
      left: 185px;
      text-align: center;
    }
  </style>
</head>

<body>
  <div class="box">
    <div class="close">X</div>
  </div>
</body>
<script>

  // 事件冒泡 是浏览器默认的事件传播机制 

  // 需求: closeBtn事件触发之后 不在向外层冒泡(阻止谁 就在谁上写)
  // 阻止事件冒泡 (阻止同类型事件向外层传播,不同类型的需要分开阻止) 

  // 常规浏览器
  // e.stopPropagation();

  // IE低版本
  // e.cancelBubble = true;   //是否阻止冒泡 (默认false:不阻止  true:阻止)

  // 兼容写法
  // if(e.stopPropagation){
  //     e.stopPropagation();
  // }else{
  //     e.cancelBubble = true;
  // }

  var box = document.querySelector('.box');
  var close = document.querySelector('.close')

  box.onclick = function () {
    console.log('点击了box');
  }

  close.onclick = function (e) {

    var e = e || window.event;

    console.log('点击了close');

    // 常规浏览器
    // e.stopPropagation();

    // IE低版本
    // e.cancelBubble = true;   //是否阻止冒泡 (默认false:不阻止  true:阻止)

    // 兼容写法
    if (e.stopPropagation()) {
      e.stopPropagation();
    } else {
      e.cancelBubble = true;
    }
  }


</script>

</html>